import 'package:flutter/material.dart';
import 'package:flutter_soundmagus/widget/eq/EqView.dart';
import 'package:flutter_soundmagus/widget/eq/eq_pd_view.dart';

/// 绘制版本号，每次绘制会+1
typedef EqWidgetVersion = int;

class EqWidgetController extends ValueNotifier<EqWidgetVersion> {
  late EqView eqView;
  EqWidgetController(super.value) {
    eqView = EqView(repaint: this); //repaint触发重回的监听器
  }

  ///刷新
  void update() {
    value++;
    
    assert(() {
      print('EqWidgetVersion $value');
      return true;
    }());
  }
}

///EQ曲线Widget，最终暴露给flutter ui的组件
class EqWidget extends StatefulWidget {
  ///选中的频点
  final int? pd;
  final int pdCount;

  ///点击频点回调
  final void Function(int pd)? onPressPd;
  final EqWidgetController controller;
  const EqWidget({
    Key? key,
    required this.controller,
    this.pd,
    required this.pdCount,
    this.onPressPd,
  }) : super(key: key);

  @override
  State<EqWidget> createState() => _EqWidgetState();
}

class _EqWidgetState extends State<EqWidget> {
  EqWidgetController get controller => widget.controller;

  @override
  Widget build(BuildContext context) {
    Widget child = Container(
      width: MediaQuery.of(context).size.width, // 屏幕宽度
      height: 150,
      // margin: const EdgeInsets.symmetric(horizontal: 5.0),
      child: CustomPaint(
        painter: controller.eqView,
      ),
    );
    child = Stack(
      children: [
        child,
        widget.pd != null
            ? EqPdView(
                pd: widget.pd!,
                onPress: widget.onPressPd,
                pdCount: widget.pdCount,
              )
            : Container()
      ],
    );
    return ValueListenableBuilder(
        valueListenable: controller,
        builder: (context, v, c) {
          return child;
        });
  }
}
